package cn.com.ddapp.ddm.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;

import java.util.List;

import cn.com.ddapp.ddm.AppConfig;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.api.OKUtilAPI;
import cn.com.ddapp.ddm.bean.order.OrderDetail;
import cn.com.ddapp.ddm.enums.OrderEnum;
import cn.com.ddapp.ddm.ui.order.AfterSalesActivity;
import cn.com.ddapp.ddm.ui.order.AfterSalesDetailActivity;

/**
 * Created by caopenghui on 2016-08-10.
 */
public class OrderDetailAdpter extends BaseAdapter {
    protected Gson gson = new Gson();
    private Context mContext;
    private List<OrderDetail> mLists ;
    public OrderDetailAdpter(List<OrderDetail> lists, Context context) {
        mContext = context;
        mLists = lists;
    }

    @Override
    public int getCount() {
        return mLists.size();
    }

    @Override
    public Object getItem(int i) {
        return mLists.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        convertView= LayoutInflater.from(mContext).inflate(R.layout.order_detail_item,null);
        final ViewHolder holder = new ViewHolder(convertView);
        final OrderDetail detail = mLists.get(position);
        String []nUrl=detail.getGoodsImg().split("\\.");
        StringBuffer buffer=new StringBuffer();
        buffer.append(nUrl[0]).append("_360.jpg");
        Glide.with(mContext).load(OKUtilAPI.imgBaseUrl+buffer).into(holder.goodsLogo);
        holder.goodsName.setText(detail.getGoodsName());
        holder.goodsNum.setText("x"+detail.getNum());
        holder.goodsPrice.setText(AppConfig.MONEY_SYMBEL+detail.getPrice());
        if(detail.getOrderStatus()== OrderEnum.PAYED.ordinal()||detail.getOrderStatus()==OrderEnum.SENDED.ordinal()||detail.getOrderStatus()==OrderEnum.ACCEPTED.ordinal()||detail.getOrderStatus()==6){
            holder.afterSales.setVisibility(View.VISIBLE);
        }
        final Integer goodsStatus = detail.getStatus();
        if(goodsStatus==6||goodsStatus==10||goodsStatus==13||goodsStatus==16){
            holder.afterSales.setText(R.string.aftersaleing);
        }else if(goodsStatus==7||goodsStatus==11||goodsStatus==14||goodsStatus==17){
            holder.afterSales.setText(R.string.aftersale_success);
        }else if(goodsStatus==8||goodsStatus==12||goodsStatus==15||goodsStatus==18){
            holder.afterSales.setText(R.string.aftersale_fail);
        }else{
            holder.afterSales.setText(R.string.action7);
        }
        holder.afterSales.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if(goodsStatus>=6&&goodsStatus<=18&&goodsStatus!=9){
                    Intent intent = new Intent(mContext, AfterSalesDetailActivity.class);
                    intent.putExtra("orderId",detail.getOrderId());
                    intent.putExtra("goodsId",detail.getGoodsId());
                    mContext.startActivity(intent);
                }else{
                    //申请售后
                    Intent intent = new Intent(mContext, AfterSalesActivity.class);
                    intent.putExtra("orderId",detail.getOrderId());
                    intent.putExtra("amount",detail.getPrice()*detail.getNum());
                    intent.putExtra("status",detail.getOrderStatus());
                    intent.putExtra("goodsId",detail.getGoodsId());
                    mContext.startActivity(intent);
                }


            }
        });
        return convertView;
    }


    static class ViewHolder{
        ImageView goodsLogo;
        TextView goodsName;
        TextView goodsPrice;
        TextView goodsNum;
        TextView afterSales;
        public ViewHolder(View view){
            goodsLogo = (ImageView)view.findViewById(R.id.goodsLogo);
            goodsName = (TextView)view.findViewById(R.id.goodsName);
            goodsPrice = (TextView)view.findViewById(R.id.goodsPrice);
            goodsNum = (TextView)view.findViewById(R.id.goodsNum);
            afterSales = (TextView)view.findViewById(R.id.after_sales);
        }
    }
}
